﻿/**
 * 大厅视图控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-5 14:19
 */
package com.addcn.texas.view
{		
	import com.addcn.texas.utils.NumberFormat;
	import com.addcn.texas.utils.PicBitMap;
	import com.adobe.utils.NumberFormatter;
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;	
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;
	import com.addcn.texas.event.HallRoomListEvent;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
		
	public class HallView extends View
	{						
		protected var _container : Object;		
		private var _app : AppLibary = AppLibary.getInstance();	
		private var _class : Class;		
		private var _current_type : int;				
		
		public function HallView(r_target : Object) {			
			_container = new Sprite();
			r_target.addChild(_container);	
		}			
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
							Main.APP_STARTUP,		
							UserInfoModel.UPDATE_USER_INFO,
							HallModel.HALLS_NAME,
							HallModel.HALL_DATA_REDY,
							HallModel.UPDATA_ROOMS,
							HallModel.UPDATA_TABLE,
							HallModel.JOIN_ROOM_OK,
							HallModel.JOIN_ROOM_ERR
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {
				case Main.APP_STARTUP:  //执行初始化				
					_intialize();	
					break;			
				case UserInfoModel.UPDATE_USER_INFO:
					_setUserInfo(r_data);
					break;
				case HallModel.HALLS_NAME:
					_setHallType(r_data);
					break;
				case HallModel.HALL_DATA_REDY:					
					break;				
				case HallModel.UPDATA_ROOMS:		
					_setupRooms(r_data);	
					sendNotification(LoadingView.HIDE_LOADING);
					break;
				case HallModel.UPDATA_TABLE:					
					
					break;
				case HallModel.JOIN_ROOM_OK:
					if (Controller.getInstance().hasCommand(joinRoomCommand)) {							
						sendNotification(joinRoomCommand, r_data);
					}
					sendNotification(LoadingView.HIDE_LOADING);
					break;
				case HallModel.JOIN_ROOM_ERR:
					sendNotification(LoadingView.HIDE_LOADING);
					sendNotification(PopupView.SHOW_ALERT_BOX, r_data);					
					break;
			}			
		}
		
		private function _intialize() : void
		{				
			//添加房间列表到显示列表
			_class = this._app.getClass("com.addcn.texas.view.components.HallDataGrid");
			if (_class) {
				var _dg =  new _class;
				_dg.x = 7;
				_dg.y = 203;
				_dg.name = "HallDataGrid";
			    _dg.addEventListener(HallRoomListEvent.REFRESH_LIST, _getRoomList);
			    _dg.addEventListener(HallRoomListEvent.CHANGE_TYPE_EVENT, _getRoomList);
			    _dg.addEventListener(HallRoomListEvent.JOIN_ROOM, _joinRoomHandler);
				_container.addChild(_dg);				
			}
			
			//添加 【马上开始玩】 按钮
			_class = this._app.getClass("NowPlayBtn");
			if (_class) {
				var _mc = new _class;				
				this._container.addChild(_mc);
				_mc.x = 14;
				_mc.y = 79;				
			}
			//添加 【简易玩法教学】 按钮
			_class = this._app.getClass("TeachBtn");
			if (_class) {
				var _teach = new _class;				
				this._container.addChild(_teach);
				_teach.x = 14;
				_teach.y = 157;				
			}
			//添加 LOGO
			_class = this._app.getClass("Logo");
			if (_class) {
				var _logo = new _class;				
				this._container.addChild(_logo);
				_logo.x = 12;
				_logo.y = 13;				
			}
			//添加 玩家信息框
			_class = this._app.getClass("com.addcn.texas.view.components.MyInfoBox");
			if (_class) {
				var _info = new _class;				
				this._container.addChild(_info);
				_info.name = "MyInfoBox";
				_info.x = 549;
				_info.y = 6;		
				_info.addEventListener(_class.CLICK_PHOTO, _myPhotoClickHandler);				
			}
			
			//添加 轮播广告
			_class = this._app.getClass("AdRotator");
			if (_class) {
				var _ad = new _class;				
				this._container.addChild(_ad);
				_ad.name = "AdRotator";
				_ad.x = 217;
				_ad.y = 4;				
			}
			
			//添加玩家信息下邊按鈕
			_class = this._app.getClass("UserInfoBtnList");
			if (_class) {
				var _btnList = new _class;
				this._container.addChild(_btnList);
				_btnList.x = 549;				
				_btnList.y = 74;
				_btnList.name = "UserInfoBtnList";
				var _label : Array = ["關于我", "訊息", "道具箱", "儲值"];
				for (var i = 0; i <= 4; i++ ) {
					if(_btnList["InfoBtn" + i]){
						_btnList["InfoBtn" + i].initialize(_label[i]);
					}
				}
			}
			
			//添加 消息框
			_class = this._app.getClass("MsgBox");
			if (_class) {
				var _msg_box = new _class;				
				this._container.addChild(_msg_box);
				_msg_box.name = "MsgBox";
				_msg_box.x = 550;
				_msg_box.y = 104;				
			}							
		}
		
		/**
		 * 设置玩家信息
		 * @param	r_data
		 */
		private function _setUserInfo(r_data : Object) : void
		{
			var _mc : Object = this._container.getChildByName("MyInfoBox");
			if (_mc) {
				if(r_data["name"]){
					_mc["NickName"].text = "歡迎!" + r_data["name"];
				}
				if(r_data["label"]){
					_mc["PlayLabel"].text = "稱號：" + r_data["label"];	
				}
				if(r_data["gold"]){
					var _fmt : NumberFormat = new NumberFormat();
					var _str : String = _fmt.format(r_data["gold"]);
					_mc["UserGold"].text = _str;				
				}
				if (r_data["pic"]) {	
					var _pic =  PicBitMap.getPicBitMap(r_data["pic"],55,55);						
					_mc.setPhoto(_pic);									
				}
			}
		}
		
		/**
		 * 点击自己的头像处理函数
		 */
		private function _myPhotoClickHandler(evt : Event) : void
		{
			
		}
		
		
		/**
		 * 设置游戏场
		 * @param	r_data
		 */
		private function _setHallType(r_data : Object) : void
		{
			var _obj : Object = this._container.getChildByName("HallDataGrid");
			if(_obj){				
				_obj.setHallType(r_data);
				
			}
			MonsterDebugger.trace("hallType",r_data);
		}
			
		/**
		 * 显示房间列表
		 * @param	r_data Object
		 */
		function _setupRooms(r_data : Object) : void
		{			
			var _list : Object = this._container.getChildByName("HallDataGrid");			
			if (_list) {	
				this._current_type = r_data["type"];
				_list.upDateList(r_data);					
			}			
		}
				
		/**
		 * 获取房间列表操作
		 * @param	evt
		 */
		private function _getRoomList(evt : HallRoomListEvent) : void
		{			
			var _data : Object = evt.params;
			var _type :int = _data["type"];
			var _request  : Boolean = true ;			
			if (_data["request"] != null) {
				_request = _data["request"];
			}								
		//	sendNotification(LoadingView.SHOW_LOADING);
			if(!Controller.getInstance().hasCommand(GetHallListCommand)){
				Controller.getInstance().addCommand(GetHallListCommand);
			}
			sendNotification(GetHallListCommand, { type:_type, request:_request } );
		}		
		
		/**
		 * 加入房间操作响应
		 * @param	evt
		 */
		private function _joinRoomHandler(evt : HallRoomListEvent) : void
		{			
			var _id : String = evt.params.rid;
			_joinRoom(_id);
		}
		/**
		 * 执行命令，进入房间
		 * @param	r_id int
		 */
		private function _joinRoom(r_id : String) : void
		{					
			sendNotification(LoadingView.SHOW_LOADING);
			if(!Controller.getInstance().hasCommand(joinRoomCommand)){
				Controller.getInstance().addCommand(joinRoomCommand);
			}
			sendNotification(joinRoomCommand, {cmd:"join", type:this._current_type, rid:r_id } );
		}
				
		override public function destroy():void 
		{					
			var _dg : Object = _container.getChildByName("HallDataGrid");
			if(_dg){
				_dg.removeEventListener(HallRoomListEvent.REFRESH_LIST, _getRoomList);
				_dg.removeEventListener(HallRoomListEvent.CHANGE_TYPE_EVENT, _getRoomList);
				_dg.removeEventListener(HallRoomListEvent.JOIN_ROOM, _joinRoomHandler);
			}
			RemoveAllChildren.remove(_container as DisplayObject);		
			_container.parent.removeChild(_container);
		}
		
	}
	
}
